<?php

require_once('BaseModel.php');

/**
 * This class stores User informations, most of them being accessible and changeable through the 'data' array
 */
class User_model extends BaseModel {

	function User_model() {
        $publicdata = array(
			'rights',
			'nickname',
			'firstname',
			'lastname',
			'email',
			'last_connection');
        parent::BaseModel('cr_users', 'user_id', $publicdata, false);
	}

	/**
	 * Loads an user based on his nickname
	 * Returns true on success, false otherwise
	 */
	function load_user_from_nick($nick) {
		$this->db->where('nickname', $nick);
		return $this->load();
	}

	/**
	 * Loads an user based on his id
	 * Returns true on success, false otherwise
	 */
	function load_user_from_id($id) {
		$this->db->where('user_id', $id);
		return $this->load();
	}

	/**
	 * Creates a new user using his infos and his password
     * Returns  true if the user was successfully created
     * 			an array mapping error messages onto bad fields if it failed 
	 */
	function new_user($infos, $pwd) {
		$checkeddata = array ();
		$checkeddata['pwdhash'] = md5($pwd);
		$checkeddata['register_date'] = date('Y-m-d');

		//checks whether this nickname is already in use
		if ($this->exists(array('nickname' => $infos['nickname'])))
    		return array('nickname' => 'This nickname is already used');
		if ($this->exists(array('email' => $infos['email'])))
    		return array('email' => 'This email is already used');

    	$result = parent::new_data($infos, $checkeddata);
    	if (is_array($result))
    		return $result;

		//reloading the user from his nickname, thus getting his id and complementary infos
		$this->load_user_from_nick($this->data['nickname']);
		return true;
	}

	/**
	 * Stores one user's information in the database
	 * Returns true if the infos got saved, false if an error occured
	 */
	function save_user($usrid, $data, $passwd = null) {
		if ($passwd != null) {
			$checked = array ();
			$checked['pwdhash'] = md5($passwd);
		}else $checked =null;
		return $this->save_data($usrid, $data, $checked);
	}

	/**
	 * Checks the validity of the infos stored in the $this->data array
	 * Returns true if each field is valid, false otherwise
	 */
	public function fields_are_valid($data) {
		$errormet = false;
		$errors = array();
		foreach ($data as $key => $value) {
			if (isset($value) && strlen($value) == 0) {
				$errors[$key] = $key.' cannot be empty';
				$errormet = true;
			}
		}
		if (isset($data['nickname']) && strlen($data['nickname']) > 30) {
			$errors['nickname'] = 'Nickname is too long';
			$errormet = true;
		}
		if (isset($data['nickname']) && strlen($data['nickname']) < 3) {
			$errors['nickname'] = 'Nickname is too short';
			$errormet = true;
		}
		if (isset($data['firstname']) && strlen($data['firstname']) > 50) {
			$errors['firstname'] = 'First name is too long';
			$errormet = true;
		}
		if (isset($data['lastname']) && strlen($data['lastname']) > 50) {
			$errors['lastname'] = 'Last name is too long';
			$errormet = true;
		}
		if (isset($data['email']) && (strlen($data['email']) > 100 
		|| !eregi('^[0-9a-z_.-]+@[0-9a-z_.-]+\.[a-z]+$', $data['email']))) {
			$errors['email'] = 'email is either too long or has an invalid format';
			$errormet = true;
		}
		if ($errormet)
			return $errors;
		return true;
	}

	/**
	 * Sets the new password
	 */
	function set_password($pwd) {
		$this->data['pwdhash'] = md5($pwd);
	}

	/**
	 * Returns an array of $count users starting with the $first
	 * If $count is null, returns all values
	 * This array is sorted on the $sorted column
	 * $match is an array containg a key and a value which is to match.
	 * ie : $match = array ($field => $namematch)
	 */
	function get_users($first = null, $count = null, $match = null, $sorted = 'nickname') {
		return $this->get_data($first, $count, $sorted, $match);
	}

	/**
	 * Returns the number of registered users
	 * $match is an array containg a key and a value which is to match.
	 * ie : $match = array ($field => $namematch)
	 */
	function get_user_count($match = null) {
		return $this->get_data_count($match);
	}

	/**
	 * Returns whether the password $pwd matches the password for the currently loaded user
	 */
	function password_match($pwd) {
		return isset ($this->data['pwdhash']) && (md5($pwd) == $this->data['pwdhash']);
	}
}
?>
